[PATCH] Apache::DBI - Disconnect on child exit

This is a multi-part message in MIME format.
--------------060709030705060906050402
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit


Currently, Apache::DBI never disconnects from the database. This leads
to the following warnings in my Postgres logs every time an Apache child
exits:

LOG: unexpected EOF on client connection

Depending on MaxRequestsPerChild this can really spam your Postgres logs.

Attached is a patch against Apache::DBI that adds a PerlChildExitHandler
in order to disconnect all existing database connections.

Comments are welcome.

Nick


--
aevum gmbh
rumfordstr. 4
80469 münchen
germany

tel: +49 89 3838 0653
http://aevum.de/

--------------060709030705060906050402
Content-Type: text/plain;
name="dbi.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="dbi.diff"

--- DBI.pm.bak 2009-07-20 16:48:35.000000000 +0200
+++ DBI.pm 2009-07-20 17:01:15.000000000 +0200
[at] [at] -39,6 +39,8 [at] [at]
# a negative value de-activates ping,
# default = 0
my %LastPingTime; # keeps track of last ping per data_source
+my $ChildExitHandlerInstalled; # set to true on installation of
+ # PerlChildExitHandler

# Check to see if we need to reset TaintIn and TaintOut
my $TaintInOut = ($DBI::VERSION >= 1.31) ? 1 : 0;
[at] [at] -134,6 +136,23 [at] [at]
}
}

+ # this PerlChildExitHandler is supposed to disconnect all open
+ # connections to the database
+ if (!$ChildExitHandlerInstalled) {
+ $ChildExitHandlerInstalled = 1;
+ my $s;
+ if (MP2) {
+ $s = Apache2::ServerUtil->server;
+ }
+ elsif (Apache->can('push_handlers')) {
+ $s = 'Apache';
+ }
+ if ($s) {
+ debug(2, "$prefix push PerlChildExitHandler");
+ $s->push_handlers(PerlChildExitHandler => \&childexit);
+ }
+ }
+
# this PerlCleanupHandler is supposed to initiate a rollback after the
# script has finished if AutoCommit is off. however, cleanup can only
# be determined at end of handle life as begin_work may have been called
[at] [at] -216,6 +235,19 [at] [at]
1;
}

+# The PerlChildExitHandler disconnects all open connections
+sub childexit {
+
+ my $prefix = "$$ Apache::DBI ";
+ debug(2, "$prefix PerlChildExitHandler");
+
+ foreach my $dbh (values(%Connected)) {
+ eval { DBI::db::disconnect($dbh) };
+ }
+
+ 1;
+}
+
# The PerlCleanupHandler is supposed to initiate a rollback after the script
# has finished if AutoCommit is off.
# Note: the PerlCleanupHandler runs after the response has been sent to

--------------060709030705060906050402--
wellnhofer [ Mo, 20 Juli 2009 17:15 ] [ ID #2009265 ]

Re: [PATCH] Apache::DBI - Disconnect on child exit

Seems like a reasonable addition to me. Apache::DBI 1.07 is kind of
broken right now (see bugs in RT), it'd be nice to get a new release
with this fixed and some of those bugs closed.

Adam


Nick Wellnhofer wrote:
> Currently, Apache::DBI never disconnects from the database. This leads
> to the following warnings in my Postgres logs every time an Apache child
> exits:
>
> LOG: unexpected EOF on client connection
>
> Depending on MaxRequestsPerChild this can really spam your Postgres logs.
>
> Attached is a patch against Apache::DBI that adds a PerlChildExitHandler
> in order to disconnect all existing database connections.
>
> Comments are welcome.
>
> Nick
>
>
>
Adam Prime [ Di, 21 Juli 2009 01:18 ] [ ID #2009351 ]

mod_perl / CGI.pm and 'our' variables

Hi

i'm new here and have a problem i have not been able to solve for 2 days
now:

i'm writing a web application using %SUBJ% and would like to set 'our
$dbh = ...' so that i do not need to pass db handler as an argument to
functions in different packages every time i need. but this 'global'
variable is not seen inside imported package as $::dbh (as it is in
normal non-mod_perl application).

Any idea?



--
Tomáš Bažant <tbazant [at] suse.cz>
Novell, SUSE Linux s.r.o.
tbazant [ Di, 21 Juli 2009 09:29 ] [ ID #2009352 ]

Re: [PATCH] Apache::DBI - Disconnect on child exit

Nick Wellnhofer napsal(a):
> Currently, Apache::DBI never disconnects from the database. This leads
> to the following warnings in my Postgres logs every time an Apache child
> exits:
>
> LOG: unexpected EOF on client connection
>
> Depending on MaxRequestsPerChild this can really spam your Postgres logs.
>
> Attached is a patch against Apache::DBI that adds a PerlChildExitHandler
> in order to disconnect all existing database connections.
>
> Comments are welcome.
>


Thanks, it made my Postgres log useful again.
jira [ Di, 21 Juli 2009 21:39 ] [ ID #2009353 ]

Re: mod_perl / CGI.pm and 'our' variables

2009/7/21 Tom=C3=A1=C5=A1 Ba=C5=BEant <tbazant [at] suse.cz>:
> i'm writing a web application using %SUBJ% and would like to set 'our
> $dbh =3D ...' so that i do not need to pass db handler as an argument to
> functions in different packages every time i need. but this 'global'
> variable is not seen inside imported package as $::dbh (as it is in
> normal non-mod_perl application).

The scoping of variables is not changed by mod_perl. When you declare
a package variable, you should be able to see it from other packages,
but you won't be able to refer to it as $dbh except in the package
where you declared it.

Example:

package Foo;
our $dbh;

$dbh->ping; # works

package Bar;
$dbh->ping; # fails

You can still use the fully-qualified name to get at it: $Foo::dbh.

- Perrin
Perrin Harkins [ Mo, 27 Juli 2009 16:37 ] [ ID #2009968 ]

Re: mod_perl / CGI.pm and 'our' variables

2009/8/5 Tom=C3=A1=C5=A1 Ba=C5=BEant <tbazant [at] suse.cz>:
> yest that works for me if all the packages are in the same file, but if
> i put the Bar package into a separate file and import it with 'use'
> directive, $::dbh always returns undef. or could it be that i am using a
> closure? i'll try to avoid closures...

It's not a closure. You just need to understand Perl's variable
naming and package rules a little better. If there's something
specific you want to show us, we could probably spot the scoping
problem.

- Perrin
Perrin Harkins [ Mi, 05 August 2009 16:45 ] [ ID #2011087 ]
Webserver » gmane.comp.apache.mod-perl » [PATCH] Apache::DBI - Disconnect on child exit

Vorheriges Thema: mod_perl Output Filters and mod_deflate
Nächstes Thema: disable input and output filters for subfolders